package com.googlecode.bluetools.common.utils;

import java.io.File;
import java.io.FileFilter;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class FileFilters {

	/**
	 * FileFilterUtils is not normally instantiated.
	 */
	private FileFilters() {

	}

	// -----------------------------------------------------------------------
	/**
	 * Returns a filter that returns true if the filename starts with the specified text.
	 * 
	 * @param prefix the filename prefix
	 * @return a prefix checking filter
	 */
	public static FileFilter prefixFileFilter(final String prefix) {
		final String _prefix = (prefix == null ? "" : prefix);
		return new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.getName().startsWith(_prefix);
			}
		};
	}

	public static FileFilter prefixFileFilter(final String[] prefixes) {
		if (prefixes == null || prefixes.length == 0) {
			return prefixFileFilter("");
		}
		FileFilter[] filters = new FileFilter[prefixes.length];
		for (int i = 0; i < prefixes.length; i++) {
			filters[i] = prefixFileFilter(prefixes[i]);
		}
		return or(filters);
	}

	/**
	 * Returns a filter that returns true if the filename ends with the specified text.
	 * 
	 * @param suffix the filename suffix
	 * @return a suffix checking filter
	 */
	public static FileFilter suffixFileFilter(final String suffix) {
		final String _suffix = (suffix == null ? "" : suffix);
		return new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(_suffix);
			}
		};

	}

	public static FileFilter suffixFileFilter(final String[] suffixes) {
		if (suffixes == null || suffixes.length == 0) {
			return suffixFileFilter("");
		}
		FileFilter[] filters = new FileFilter[suffixes.length];
		for (int i = 0; i < suffixes.length; i++) {
			filters[i] = suffixFileFilter(suffixes[i]);
		}
		return or(filters);
	}

	/**
	 * Returns a filter that returns true if the filename matches the specified text.
	 * 
	 * @param name the filename
	 * @return a name checking filter
	 */
	public static FileFilter nameFileFilter(final String name) {
		return new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.getName().equals(name);
			}
		};
	}

	/**
	 * Returns a filter that checks if the file is a directory.
	 * 
	 * @return directory file filter
	 */
	public static FileFilter directoryFileFilter() {
		return new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.exists() && pathname.isDirectory();
			}
		};
	}

	// -----------------------------------------------------------------------
	/**
	 * Returns a filter that ANDs the two specified filters.
	 * 
	 * @return a filter that ANDs the two specified filters
	 */
	public static FileFilter and(final FileFilter[] filters) {
		return new FileFilter() {
			public boolean accept(File pathname) {
				boolean result = true;
				for (int i = 0; i < filters.length; i++) {
					result = result && filters[i].accept(pathname);
				}
				return result;
			}
		};
	}

	/**
	 * Returns a filter that ORs the two specified filters.
	 * 
	 * @return a filter that ORs the two specified filters
	 */
	public static FileFilter or(final FileFilter[] filters) {
		return new FileFilter() {
			public boolean accept(File pathname) {
				boolean result = false;
				for (int i = 0; i < filters.length; i++) {
					result = (result || filters[i].accept(pathname));
				}
				return result;
			}
		};
	}

	/**
	 * Returns a filter that NOTs the specified filter.
	 * 
	 * @param filter the filter to invert
	 * @return a filter that NOTs the specified filter
	 */
	public static FileFilter not(final FileFilter filter) {
		return new FileFilter() {
			public boolean accept(File pathname) {
				return !filter.accept(pathname);
			}
		};
	}

	public static void main(String[] args) {

	}
}
